function fillDepartment() {
	var depts = $("#deptId");

	$.getJSON("admin/department/department!bytype", {
		"bean.id" : "root",
		"bean.departmentType" : "Manufacture"
	}, function(data, textStatus, jqXHR) {
		$.each(data, function(i, item) {
			var lb = $("<li><label><input name='bean.providers["
					+ i
					+ "].id' type='checkbox' value='"
					+ item.id
					+ "' id='check_"
					+ i
					+ "' "
					+ ($.inArray(item.id, indepts) == -1 ? ""
							: "checked='checked'") + " />" + item.name
					+ "</label></li>");
			depts.append(lb);

		});
	});
}
function saveProduct() {
	var postdata = $("form").serialize();
	$.ajax({
		type : "POST",
		url : 'product/product!' + action,
		data : postdata,
		success : function(msg) {
			createNotify('保存产品信息成功!');
		},
		error : function(msg) {
			createNotify("保存产品出错:\n" + msg + "\n请把这个消息报告给管理员[69625]");
		}
	});
}
var cache = {}, lastXhr = {};
var tmpQuoa = null;
$(function() {
	

	setAutoCode($("#pcode"),true);
	setAutoCode($("#pcode2"),false);

	// 显示某个产品
	// doAjaxLink("${base}getproduct.action?product.id=" + id);
	$("#punit").val(punit);
	$("#ptype").val(ptype);
	fillDepartment();
	$("#saveaction").click(function() {
		saveProduct();
	}).button();

	$("#viewquota").bind("click", function() {
		var bid = $(this).attr("bid");
		$.getJSON("product/product!queryquotas", {
			"term" : bid
		}, function(data) {
			$("#uquotas").empty();

			for ( var x = 0; x < data.length; x++) {
				var opt = $("<label><input class='qcheck' type='checkbox' value='"+data[x].quotaId+"'/>"+data[x]['owner'].code + "[" + data[x].consume + "]"+"</label>");
				$("#uquotas").append(opt).append("<br/>");
			}

		});
	});
	$("#quotabtn").bind("click",function(){
		if(tmpQuoa==null){
			alert("请选择产品");
			return;
		}
		if($("#consume").val()==null || $("#consume").val()==0)
			$("#consume").val("1");
		var pstr = "quotabeans[0].owner.code="+tmpQuoa.code+"&quotabeans[0].host.code="+pdtid+"&quotabeans[0].consume="+$("#consume").val();
		var url="product/product!createquotas";
		//alert(pstr);
		doSubmit(url,pstr,function(){
			$("#viewquota").trigger("click");
		});
		
	});
	$("#delquota").bind("click",function(){
		if(!confirm("确认要删除这些定额吗?")){
			return;
		}
		var vals = $("#uquotas input:checked");
		var pstr = "";
	    vals.each(function(i,item){
	    	pstr+="quotabeans["+i+"].id="+item.value+"&";
	    });
	    var url="product/product!removequotas";
	    doSubmit(url,pstr,function(){
	    	$("#viewquota").trigger("click");
	    });
	});
});

function setAutoCode(jqobj, selected) {
	if(jqobj==null || typeof(jqobj)=='undefined')return;
	jqobj.autocomplete(
			{
				minLength:3,
				source :"product/product!search",
				focus : function(event, ui) {
					jqobj.val(ui.item.code);
					return false;
				},
				select : function(event, ui) {
					if (selected) {
						var id = ui.item.code;
						var url = 'product/product!get' + "?bean.code=" + id;
						doAjaxLink(url);							
					}else{
						tmpQuoa = ui.item;
					}
					
					return false;
				}

			}).data("autocomplete")._renderItem = function(ul, item) {
		return $("<li></li>").data("item.autocomplete", item).append(
				"<a>" + item.code + "|" + item.name + "</a>").appendTo(ul);
	};
}